package com.itheima.bos.web.action;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.ws.rs.core.MediaType;

import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.itheima.bos.constant.Constants;
import com.itheima.bos.domain.base.Courier;
import com.itheima.bos.domain.base.FixedArea;
import com.itheima.bos.service.FixedAreaService;
import com.itheima.bos.web.action.common.BaseAction;
import com.itheima.crm.domain.Customer;

@ParentPackage(value="json-default")
@Namespace("/fixedArea")
@Controller
@Scope("prototype")
public class FixedAreaAction extends BaseAction<FixedArea> {

	@Autowired
	private FixedAreaService fixedAreaService;
	
	@Action(value="fixedArea_save",results={@Result(name="success",type="redirect",location="../pages/base/fixed_area.html")})
	public String fixedArea_save(){
		fixedAreaService.save(model);
		return SUCCESS;
	}
	
	@Action(value="fixedArea_pageQuery",results={@Result(name="success",type="json")})
	public String area_pageQuery(){
		Pageable pageable = new PageRequest(page - 1, rows);
		Specification<FixedArea> spec = new Specification<FixedArea>() {

			@Override
			public Predicate toPredicate(Root<FixedArea> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<Predicate>();
				Predicate predicate = null;
				if(StringUtils.isNotBlank(model.getId())){
					predicate = cb.equal(root.get("id").as(String.class), model.getId());
					list.add(predicate);
				}
				if(StringUtils.isNotBlank(model.getCompany())){
					predicate  = cb.like(root.get("company").as(String.class), "%" + model.getCompany() + "%");
					list.add(predicate);
				}
				if(list.size() > 0 && list != null){
					Predicate ps[] = new Predicate[list.size()];
					query.where(list.toArray(ps));
				}
				query.orderBy(cb.asc(root.get("id").as(String.class)));
				return query.getRestriction();
			}
		};
		Page<FixedArea> page = fixedAreaService.pageQuery(spec,pageable);
		pushPageDateToValueStack(page);
		return SUCCESS;
	}
	
	@Action(value="fixedArea_noassociationSelect",results={@Result(name="success",type="json")})
	public String fixedArea_noassociationSelect(){
		Collection<? extends Customer> customers = WebClient.create(Constants.CRM_MANAGEMENT_URL + "/services/customerService/noassociationcustomers")
				.accept(MediaType.APPLICATION_JSON)
				.getCollection(Customer.class);
		ServletActionContext.getContext().getValueStack().push(customers);
		return SUCCESS;
	}
	
	@Action(value="fixedArea_associationSelect",results={@Result(name="success",type="json")})
	public String fixedArea_associationSelect(){
		Collection<? extends Customer> customers = WebClient.create(Constants.CRM_MANAGEMENT_URL + "/services/customerService/associationcustomers?fixedAreaId=" + model.getId())
				.accept(MediaType.APPLICATION_JSON)
				.getCollection(Customer.class);
		ServletActionContext.getContext().getValueStack().push(customers);
		return SUCCESS;
	}
	
	private String[] customerIds;
	
	public void setCustomerIds(String[] customerIds) {
		this.customerIds = customerIds;
	}
	
	/**
	 * 定区关联客户
	 * @return
	 */
	@Action(value="fixedArea_associationCustomersToFixedArea",results={@Result(name="success",type="redirect",location="../pages/base/fixed_area.html")})
	public String fixedArea_associationCustomersToFixedArea(){
		String customerStr = StringUtils.join(customerIds,",");
		String fixedAreaId = model.getId();
		WebClient.create(Constants.CRM_MANAGEMENT_URL + "/services/customerService/associationCustomersToFixedArea?customerStr=" + customerStr + "&fixedAreaId=" + fixedAreaId)
				.type(MediaType.APPLICATION_JSON)
				.put(null);
		return SUCCESS;
	}
	
	private String courierId;
	
	private String takeTimeId;

	public void setCourierId(String courierId) {
		this.courierId = courierId;
	}

	public void setTakeTimeId(String takeTimeId) {
		this.takeTimeId = takeTimeId;
	}

	/**
	 * 定区关联快递员
	 * @return
	 */
	@Action(value="fixedArea_associationCourierToFixedArea",results={@Result(name="success",type="redirect",location="../pages/base/fixed_area.html")})
	public String fixedArea_associationCourierToFixedArea(){
		fixedAreaService.associationCourierToFixedArea(model.getId(), courierId, takeTimeId);
		return SUCCESS;
	}
	
	
	/**
	 * 根据定区ID查询所有的快递员
	 */
	@Action(value="fixedArea_associationCourier",results={@Result(name="success",type="json")})
	public String fixedArea_associationCourier(){
		List<Courier> list = fixedAreaService.associationCourier(model.getId());
		ServletActionContext.getContext().getValueStack().push(list);
		return SUCCESS;
	}
}
